package com.ming;

import java.awt.*;

public class Line {

    //起始坐标
    int x = 380;
    int y = 180;

    //终点坐标
    int endx = 500;
    int endy = 500;

    //线长
    double length = 30;

    double MIN_length = 100;

    double MAX_length = 650;
    double n = 0;

    //方向
    int dir = 1;

    //状态 0 摇摆 1 抓取 2 收回 3 抓取返回
    int status;

    Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");

    GameWin frame;

    Line (GameWin frame){
        this.frame = frame;
    }


    //碰撞检测
    void logic(){
        for (Object obj:this.frame.objectList){
            if (endx > obj.x && endx < obj.x + obj.width &&
                    endy > obj.y && endy < obj.y + obj.height){
                status = 3;
                obj.flag = true;
            }
        }
    }

    //绘制线条
    void lines(Graphics g){
        endx = (int) (x + length*Math.cos(n*Math.PI));
        endy = (int) (y + length*Math.sin(n*Math.PI));
        g.setColor(Color.red);
        g.drawLine(x-1,y,endx-1,endy);
        g.drawLine(x,y,endx,endy);
        g.drawLine(x+1,y,endx+1,endy);
        g.drawImage(hook,endx-36,endy-2,null);
    }

    //线条行为
    void paintSelf(Graphics g){
        logic();
        switch (status){
            case 0:
                //遇到边界反向，避免转圈
                if (n<0) {dir = 1;}
                else if (n>1){dir = -1;}
                n = n + 0.005 * dir;
                lines(g);
                break;
            case 1: //抓取
                //抓取到指定长度
                if (length<=MAX_length){
                    length = length + 10;
                    lines(g);
                }else {
                    status = 2;
                }
                break;
            case 2: //收回
                if (length>=MIN_length){
                    length = length - 10;
                    lines(g);
                }else {
                    status = 0;
                }
                break;
            case 3:
                int m = 1;
                if (length>=MIN_length){
                    length = length - 10;
                    lines(g);
                    for (Object obj:this.frame.objectList){
                        if (obj.flag){
                            m = obj.m;
                            obj.x = endx - obj.getWidth()/2;
                            obj.y = endy;
                            if (length <= MIN_length){
                                obj.x = -150;
                                obj.y = -150;
                                obj.flag = false;
                                Bg.waterFlag = false;
                                //统计抓取到的总积分
                                Bg.score += obj.score;
                                status = 0;
                            }
                            if (Bg.waterFlag){
                                if (obj.type == 1){
                                    m = 1;
                                }
                                if (obj.type == 2){
                                    obj.x = -150;
                                    obj.y = -150;
                                    obj.flag = false;
                                    Bg.waterFlag  = false;
                                    status = 2;
                                }
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                break;
            default:
        }
    }
}
